package com.gjxy.controller;

import com.gjxy.entity.*;
import com.gjxy.service.SeCourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

class ClassForGetCoursesByTeacherName{
    static class ClassForGrade{
        static class Student {
            private int id;
            private String num;
            private String name;

            public int getId() {
                return id;
            }
            public void setId(int id) {
                this.id = id;
            }
            public String getNum() {
                return num;
            }
            public void setNum(String num) {
                this.num = num;
            }
            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
        }
        private String index;
        private String class_name;
        private List<Student> member;

        public String getIndex() {
            return index;
        }
        public void setIndex(String index) {
            this.index = index;
        }
        public String getClass_name() {
            return class_name;
        }
        public void setClass_name(String class_name) {
            this.class_name = class_name;
        }
        public List<Student> getMember() {
            return member;
        }
        public void setMember(List<Student> member) {
            this.member = member;
        }
    }
    private int id;
    private String index;
    private String course_name;
    private List<ClassForGrade> have_this_course_class;

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getIndex() {
        return index;
    }
    public void setIndex(String index) {
        this.index = index;
    }
    public String getCourse_name() {
        return course_name;
    }
    public void setCourse_name(String course_name) {
        this.course_name = course_name;
    }
    public List<ClassForGrade> getHave_this_course_class() {
        return have_this_course_class;
    }
    public void setHave_this_course_class(List<ClassForGrade> have_this_course_class) {
        this.have_this_course_class = have_this_course_class;
    }
}
@Controller
public class SeCourseController {

    @Autowired
    SeCourseService seCourseService;

    @RequestMapping("/course/getCoursesByTeacherName/{teacherName}")
    @ResponseBody
    public ResultInfo getCoursesByTeacherName(@PathVariable("teacherName")String teacherName) throws NoSuchMethodException {
        ResultInfo resultInfo = new ResultInfo();
        List<ClassForGetCoursesByTeacherName> res = new ArrayList<>();
        List<SeCourse> b = seCourseService.getCoursesByTeacherName(teacherName);
        for(int i = 0; i < b.size(); i++) {
            ClassForGetCoursesByTeacherName z = new ClassForGetCoursesByTeacherName();
            z.setCourse_name(b.get(i).getName());
            z.setId(i + 1);
            z.setIndex((i + 1) + "");
            List<SeCourseClass> c = seCourseService.getClassesByCourseId(b.get(i).getCode());
            List<ClassForGetCoursesByTeacherName.ClassForGrade> grades = new ArrayList<>();
            for(int j = 0; j < c.size(); j++) {
                ClassForGetCoursesByTeacherName.ClassForGrade d = new ClassForGetCoursesByTeacherName.ClassForGrade();
                d.setIndex((i + 1) + "-" + (j + 1));
                SeGrade e = seCourseService.getGradeById(c.get(j).getClassId());
                if(e == null){
                    continue;
                }
                d.setClass_name(e.getCName());
                List<SeUser> f = seCourseService.getUsersByCid(e.getCId());
                List<ClassForGetCoursesByTeacherName.ClassForGrade.Student> students = new ArrayList<>();
                for(int k = 0; k < f.size(); k ++) {
                    ClassForGetCoursesByTeacherName.ClassForGrade.Student student = new ClassForGetCoursesByTeacherName.ClassForGrade.Student();
                    student.setId(k + 1);
                    student.setName(f.get(i).getUName());
                    student.setNum(f.get(i).getUNum());
                    students.add(student);
                }
                d.setMember(students);
                grades.add(d);
            }
            z.setHave_this_course_class(grades);
            res.add(z);
        }
        resultInfo.setResult(res);
        return resultInfo;
    }

}
